﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using Microsoft.Office.Interop.Word;

public partial class Police_TranslateApprove_IntroduceLetter : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Bind();
    }
    private void Bind()
    {
        if (Request["ID"] == null)
            return;

        Yipusoft.SoldierArchives.Model.TranslateIntroLetterInfo info = Yipusoft.SoldierArchives.BLL.TranslateIntroLetter.GetDetail(new Guid(Request["ID"]));
        if (info == null)
            return;
        lblAmyDate2.Text = lblAmyDate.Text = info.DateArmy.Year == 9999 ? "" : info.DateArmy.ToString("yyyy年M月d日");
        lblApproveMan.Text = info.ApproveMan;
        lblDay.Text = info.DateRegister.Day.ToString();
        lblDuty.Text = info.Duty;
        lblID2.Text = lblID.Text = info.Number.ToString();
        lblLinkMan.Text = info.Linkman;
        lblMonth.Text = info.DateRegister.Month.ToString();
        lblName2.Text = lblName.Text = info.SoldierName;
        lblNewDepartment.Text = info.NewDepartment;
        lblOldDepartment.Text = info.OldDepartment2;
        lblOverDay.Text = info.TimeLimit.ToString();
        lblRank2.Text = lblRank.Text = info.Ranker.ToString();
        lblRemark.Text = info.Remark.ToString();
        lblSex2.Text = lblSex.Text = info.Sex;
        lblUndertaker.Text = info.Undertaker;
        lblDepartment.Text = info.NewDepartment;
        lblDate.Text = ConvertNumber(info.DateBook.ToString("yyyy年M月d日"));
        lblYearID2.Text = lblYearID.Text = info.YearNo.ToString();
        lblFromDuty.Text = info.OldDepartment;
        lblOverDay.Text = info.DateRegister.ToString("yyyy年M月d日");
        lblTakeType0.Text = lblTakeType.Text = info.TakeType;


    }

    protected void Button2_Click(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ExportData("application/ms-Word", DateTime.Now.ToString("yyMdHm") + ".doc");
    }

    private void ExportData(string FileType, string FileName)
    {

    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }
    protected void BtnExprot_Click(object sender, EventArgs e)
    {
        if (Request["ID"] == null)
            return;
        Yipusoft.SoldierArchives.Model.TranslateIntroLetterInfo info = Yipusoft.SoldierArchives.BLL.TranslateIntroLetter.GetDetail(new Guid(Request["ID"]));

        if (info == null)
            return;
        killAllProcess();
        object oMissing = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Word._Application oWord;
        Microsoft.Office.Interop.Word._Document oDoc;
        string FileName2 = DateTime.Now.ToString("yyyyMMddhhmmss") + ".doc";
        object Filename = @Server.MapPath("Doc/") + FileName2;
        //try
        //{
        object missing = System.Reflection.Missing.Value;
        object oTemplate = Server.MapPath("Template/IntroduceLetter.dot");
        oWord = new Microsoft.Office.Interop.Word.Application();
        oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);

        Object Nothing = System.Reflection.Missing.Value;
        object Visible = true;
        object ReadOnly = false;
        oWord.Visible = false;

        foreach (Microsoft.Office.Interop.Word.Bookmark bk in oDoc.Bookmarks)
        {
            switch (bk.Name)
            {

                case "YearID":
                    bk.Select();
                    bk.Range.Text = "[" + info.YearNo.ToString() + "]";
                    break;
                case "YearID2":
                    bk.Select();
                    bk.Range.Text = "[" + info.YearNo.ToString() + "]";
                    break;
                case "WordID":
                    bk.Select();
                    bk.Range.Text = info.Number.ToString();
                    break;
                case "WordID2":
                    bk.Select();
                    bk.Range.Text = info.Number.ToString();
                    break;
                case "Name":
                    bk.Select();
                    bk.Range.Text = info.SoldierName;
                    break;
                case "Name2":
                    bk.Select();
                    bk.Range.Text = info.SoldierName;
                    break;
                case "Take":
                    bk.Select();
                    bk.Range.Text = info.TakeType.ToString();
                    break;
                case "Sex":
                    bk.Select();
                    bk.Range.Text = info.Sex.ToString();
                    break;
                case "Sex2":
                    bk.Select();
                    bk.Range.Text = info.Sex.ToString();
                    break;
                case "OverDay":
                    bk.Select();
                    bk.Range.Text = info.DateRegister.ToString("yyyy年M月d日");
                    break;
                case "Duty":
                    bk.Select();
                    bk.Range.Text = info.Duty;
                    break;
                case "AmyDate2":
                    bk.Select();
                    bk.Range.Text = info.DateArmy.Year == 9999 ? "" : info.DateArmy.ToString("yyyy年M月d日");
                    break;
                case "Rank2":
                    bk.Select();
                    bk.Range.Text = info.Ranker;
                    break;
                case "MonthDay":
                    bk.Select();
                    bk.Range.Text = info.DateRegister.ToString("M月d日");
                    break;
                case "OldDepartment":
                    bk.Select();
                    bk.Range.Text = info.OldDepartment.ToString();
                    break;
                case "NewDepartment":
                    bk.Select();
                    bk.Range.Text = info.NewDepartment.ToString();
                    break;
                case "OldDepartment2":
                    bk.Select();
                    bk.Range.Text = info.OldDepartment.ToString();
                    break;
                case "NewDepartment2":
                    bk.Select();
                    bk.Range.Text = info.NewDepartment.ToString();
                    break;
                case "Department":
                    bk.Select();
                    bk.Range.Text = info.NewDepartment;
                    break;
                case "LinkMan":
                    bk.Select();
                    bk.Range.Text = info.Linkman.ToString();
                    break;
                case "Archives":
                    bk.Select();
                    bk.Range.Text = info.TakeType.ToString();
                    break;
                case "undertaker":
                    bk.Select();
                    bk.Range.Text = info.Undertaker.ToString();
                    break;
                case "ApproveMan":
                    bk.Select();
                    bk.Range.Text = info.ApproveMan;
                    break;
                case "Rank":
                    bk.Select();
                    bk.Range.Text = info.Ranker;
                    break;
                case "Remark":
                    bk.Select();
                    bk.Range.Text = info.Remark;
                    break;
                case "AmyDate":
                    bk.Select();
                    bk.Range.Text = info.DateArmy.Year == 9999 ? "" : info.DateArmy.ToString("yyyy年M月d日");
                    break;
                case "Date":
                    bk.Select();
                    bk.Range.Text = ConvertNumber(info.DateBook.ToString("yyyy年M月d日"));
                    break;
            }


        }
        object oFormat = WdSaveFormat.wdFormatDocument97;
        oDoc.SaveAs(ref Filename, ref oFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

        //    oWord.Documents.Open(ref Filename, ref missing, ref ReadOnly, ref missing,
        //ref missing, ref missing, ref missing, ref missing,
        //ref missing, ref missing, ref missing, ref Visible,
        //ref missing, ref missing, ref missing,
        //ref missing);
        //oDoc.PrintOut(ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
        //oDoc.PrintPreview();
        //openDocObj = new ActiveXObject("SharePoint.OpenDocuments.2");
        oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
        oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
        //}
        //catch
        //{

        //};
        // Response.Redirect("http://10.24.10.13/Police/TranslateDocument/Doc/" + FileName2.ToString());
        string path = Filename.ToString();
        FileInfo file = new FileInfo(Filename.ToString());
        FileStream myfileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
        byte[] filedata = new Byte[file.Length];
        myfileStream.Read(filedata, 0, (int)(file.Length));
        myfileStream.Close();
        Response.Clear();
        Response.ContentType = "application/msword";
        Response.AddHeader("Content-Disposition", "attachment;filename=Report.doc");
        Response.Flush();
        Response.BinaryWrite(filedata);
        Response.End();
    }
    private string ConvertNumber(string Number)
    {

        string[] BigNumber = new string[] { "十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", "二十一", "二十二", "二十三", "二十四", "二十五", "二十六", "二十七", "二十八", "二十九", "三十", "三十一", "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        string[] SmallNumber = new string[] { "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
        string ReturnStr = string.Empty;
        for (int i = 0; i < 10; i++)
        {
            Number = Number.Replace(SmallNumber[i], BigNumber[i]);
        }
        return ConvertNumber1(Number);
    }

    private string ConvertNumber1(string Number)
    {

        string[] BigNumber = new string[] { "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        string[] SmallNumber = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
        string ReturnStr = string.Empty;
        for (int i = 0; i < 10; i++)
        {
            Number = Number.Replace(SmallNumber[i], BigNumber[i]);
        }
        return Number;
    }

    protected void killAllProcess() // 杀掉所有winword.exe进程
    {
        System.Diagnostics.Process[] myPs;
        myPs = System.Diagnostics.Process.GetProcesses();
        foreach (System.Diagnostics.Process p in myPs)
        {
            if (p.Id != 0)
            {
                string myS = "WINWORD.EXE" + p.ProcessName + "  ID:" + p.Id.ToString();
                try
                {
                    if (p.Modules != null)
                        if (p.Modules.Count > 0)
                        {
                            System.Diagnostics.ProcessModule pm = p.Modules[0];
                            myS += "\n Modules[0].FileName:" + pm.FileName;
                            myS += "\n Modules[0].ModuleName:" + pm.ModuleName;
                            myS += "\n Modules[0].FileVersionInfo:\n" + pm.FileVersionInfo.ToString();
                            if (pm.ModuleName.ToLower() == "winword.exe")
                                p.Kill();
                        }
                }
                catch
                { }
                finally
                {
                    ;
                }
            }
        }
    }
}
